

var elementGroup = {};

var enums = {
    fog_wn: {"0": "正常", "1": "报警 "},
    water_wn: {"0": "报警", "1": "正常"}
};

var keyStra = {
    "fog_wn": enums.fog_wn,
    "water_wn": enums.water_wn
};
//获取DOM
function getElements(key) {
    if (!elementGroup[key]) {
        elementGroup[key] = $("div[data-dev-body-id='" + curEquipId + "'] input[date_pram_id=" + key + "]")[0];
    }
    return elementGroup[key];
}
// 改变input参数的值
function changeValue(key, value) {
    if (getElements(key) == null) {
        return;
    }
        getElements(key).value = processValue(key, value);
}
// 填写input参数的值
function processValue(key, value) {
    if (keyStra[key]) {
        return keyStra[key][value];
    } else {
        return value;
    }
}


function processDate(data) {
    // removeLoading($("section[data-id='" + curEquipId + "']"));
    if (data[curStationCode]){
        if (data[curStationCode][curAreaId]) {
            if (data[curStationCode][curAreaId][curEquipId]) {
                $.each(data[curStationCode][curAreaId][curEquipId], function (i, n) {
                    changeValue(i, n);
                });
            }
            if (data[curStationCode][curAreaId][humTemSensorId]) {
                sensorProcess1(data[curStationCode][curAreaId][humTemSensorId]);
            }
            if (data[curStationCode][curAreaId][enviornmentSensorId]) {
                sensorProcess2(data[curStationCode][curAreaId][enviornmentSensorId]);
            }
        }
    }

}

function sensorProcess1(msg) {
    if (msg.temp) {
        $("#thermometer").val(msg.temp);
        g.refresh(msg.temp);
    }
    if (msg.hum) {
        $("#humidity").val(msg.temp);
        g2.refresh(msg.hum);
    }
}
function sensorProcess2(msg) {
    $("#smoke_warn"+curAreaId).removeClass("wh-col");
    if (msg.fog_wn) {
        if (msg.fog_wn == "0") {
            $("#smoke_warn"+curAreaId).removeClass("blink");
            $("#smoke_warn"+curAreaId).addClass("gr-col");
        } else {
            $("#smoke_warn"+curAreaId).removeClass("gr-col");
            $("#smoke_warn"+curAreaId).addClass("blink");
        }
    }
    if (msg.water_wn) {
        $("#water_warn"+curAreaId).removeClass("wh-col");
        if (msg.water_wn == "1") {
            $("#water_warn"+curAreaId).removeClass("blink").addClass("gr-col");
        } else {
            $("#water_warn"+curAreaId).removeClass("gr-col").addClass("blink");
        }
    }
}
// 告警信息
function processWarningData(data) {
    if (data[curStationCode]) {
        var content = "";
        //事件信息
        if (data[curStationCode]["0"]) {
            $(data[curStationCode]["0"]).each(function (a) {
                $(data[curStationCode][a]).each(function (i, n) {
                    var info = n.wn == 0 ? '事件' : n.wn;
                    content += "<tr><td>" + n.cd + "</td><td>" + n.sg + "</td><td>" + n.dn + "</td><td><span class='label label-default'>" + info + "</span></td><td>" + n.rm + "</td></tr>";
                });
            });
            isShowWarningInfo($(".alarm"), content);
        }
        //告警信息
        if (data[curStationCode]["1"]) {
            $(data[curStationCode]["1"]).each(function (a) {
                $(data[curStationCode][a]).each(function (i, n) {
                    var info = n.wn == 1 ? '告警' : n.wn;
                    content += "<tr><td>" + n.cd + "</td><td>" + n.sg + "</td><td>" + n.dn + "</td><td><span class='label label-warning'>" + info + "</span></td><td>" + n.rm + "</td></tr>";
                });
            });
            isShowWarningInfo($(".alarm"), content);
        }
        //故障信息
        if (data[curStationCode]["2"]) {
            var utteranceStr = "";
            $(data[curStationCode]["2"]).each(function (a) {
                $(data[curStationCode]["2"][a]).each(function (i, n) {
                    var info = n.wn == 2 ? '故障' : n.wn;

                    content += "<tr><td>" + n.cd + "</td><td>" + n.sg + "</td><td>" + n.dn + "</td><td><span class='label label-danger'>" + info + "</span></td><td>" + n.rm + "</td></tr>";
                    utteranceStr +=n.dn + info + n.rm +"。" ;
                });
            });
            if (utteranceStr.length > 0) {
                fireUtteranceEvent(utteranceStr);
                utteranceStr = "";
            }
            isShowWarningInfo($(".alarm"), content);
            // transformScroll();
        }
        if(content==''){
            isShowWarningInfo($(".alarm"), '')
        }
        if (data[curStationCode]["iscon"]) {
            $.each(data[curStationCode]["iscon"],function(a){
                $.each(data[curStationCode]["iscon"][a],function (i, n) {
                    setConnStatus(a,n);
                });
            });
        }
    }
}

function showWarningInfo(selector) {
    selector.children(".scrollContainer").show();
    selector.children(".nonInfo").hide();
}

function showNonWarningInfo(selector) {
    selector.children(".scrollContainer").hide();
    selector.children(".nonInfo").show();
}

function isShowWarningInfo(selector, content) {
    if (content.length == 0) {
        showNonWarningInfo(selector);
    } else {
        shortLoading(selector);
        selector.find("tbody").html(content);
    }
}

function shortLoading(selector) {
    addLoading(selector);
    setTimeout(function () {
        removeLoading(selector);
        showWarningInfo(selector);
    }, 1000);
}

function setConnStatus(id,n){
    setTimeout(function(){
        $.each(n,function(i,j){
            if(j=='off'){
                $('#s'+id+" button[data-equip-id=" + i + "]").removeClass('btn-info').addClass("btn-default").attr('disabled','disabled').css('opacity','1');
            }else{
                $('#s'+id+" button[data-equip-id=" + i + "]").removeClass('btn-default').addClass("btn-info").removeAttr('disabled','disabled');
            }
        });
    },2000)
}
